package com.itheima.homework.API;

import com.itheima.homework.DTO.WeatherDTO;
import com.itheima.homework.utils.WeatherHttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 *
 *
 * @author blusr
 * @version 1.0
 * @date 2025/10/8 19:06
 */
@SuppressWarnings("all")
public class WeatherAPI {
    public static String getLiveWeather_24Hours(WeatherDTO weatherDTO){
        try {
            String path = "/api/weather/real/time";
            return getResult(path,weatherDTO);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getSevenDaysWeather(WeatherDTO weatherDTO){
        try {
            String path = "/api/weather/seven/days";
            return getResult(path,weatherDTO);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getFifteenDaysFutureWeather(WeatherDTO weatherDTO){
        try {
            String path = "/api/weather/fifteen/forty";
            return getResult(path,weatherDTO);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String getResult(String path, WeatherDTO weatherDTO){
        String host = "https://kztq.market.alicloudapi.com";
        String method = "POST";
        String appcode = "6acf51c8f65b4f0ab844d6fdbce0ae53";
        Map<String, String> HEADERS = new HashMap<String, String>();
        HEADERS.put("Authorization", "APPCODE " + appcode);
        HEADERS.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("ip", weatherDTO.getIp());
        bodys.put("city", weatherDTO.getCity());
        bodys.put("province", weatherDTO.getProvince());
        bodys.put("cityid", weatherDTO.getCityid());
        bodys.put("adcode", weatherDTO.getAdcode());
        try {
            HttpResponse response = WeatherHttpUtils.doPost(host,path,method, HEADERS, querys, bodys);
            return EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
